package com.olinsports.system.olinsportssystem.util;

import com.olinsports.system.olinsportssystem.model.InventoryDetails;
import com.olinsports.system.olinsportssystem.model.SavingDetail;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


public class SavingDetailExcel extends AbstractExcelView {
	private String mSheetName;
	private String mFileName;
	ByteArrayOutputStream byteArrayOut = null;
	BufferedImage bufferImg = null;



	public SavingDetailExcel(String sheetName, String fileName){
		mSheetName = sheetName;
		mFileName = fileName;
	}
	
	// /upload/"+commodities[i].coid+"/"+commodities[i].cophoto
	
	@Override
	public void buildExcelDocument(Map<String, Object> obj,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		HSSFSheet sheet = workbook.createSheet(mSheetName);//生成的excel sheet的名称为list
		//画图的顶级管理器，一个sheet只能获取一个（一定要注意这点）  
//		HSSFPatriarch patriarch = sheet.createDrawingPatriarch();   
		
		HSSFCell cell = getCell(sheet, 0, 0);//第一行第一列对象
		HSSFCellStyle dateStyle = workbook.createCellStyle();
		// dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("mm/dd/yyyy"));
		cell = getCell(sheet, 0, 0);//第二行第一列对象
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		String dateTimeStr = "表格生成日期：" + df.format(new Date());
		
		HSSFFont font = workbook.createFont();
		font.setColor(HSSFFont.COLOR_RED);
		dateStyle.setFont(font);
		
		cell.setCellValue(dateTimeStr);
		
		for(int i = 0;i<30;i++){
			getCell(sheet, 1, i).setCellStyle(dateStyle);
		}
		getCell(sheet, 1, 0).setCellValue("日期");
		getCell(sheet, 1, 1).setCellValue("操作类型");
		getCell(sheet, 1, 2).setCellValue("会员号");
		getCell(sheet, 1, 3).setCellValue("会员姓名");
		getCell(sheet, 1, 4).setCellValue("金额");
		getCell(sheet, 1, 5).setCellValue("操作员");
		
		////设置默认宽度、高度值
		sheet.setDefaultColumnWidth(20);  
		sheet.setDefaultRowHeightInPoints(30);
		
		List<SavingDetail> savingDetails = (List<SavingDetail>)obj.get("inventoryDetailss");
		for (int i = 0; i < savingDetails.size(); i++) {
			getCell(sheet, 1+(i+1), 0).setCellValue(df.format(savingDetails.get(i).getDate()));
			getCell(sheet, 1+(i+1), 1).setCellValue(savingDetails.get(i).getType());
			getCell(sheet, 1+(i+1), 2).setCellValue(savingDetails.get(i).getVipNum());
			getCell(sheet, 1+(i+1), 3).setCellValue(savingDetails.get(i).getVipName());
			getCell(sheet, 1+(i+1), 4).setCellValue(savingDetails.get(i).getAmount());
			getCell(sheet, 1+(i+1), 5).setCellValue(savingDetails.get(i).getOperatorNum().getUsrName());
		
		}
		String filename = mFileName+".xls";// 设置下载时客户端Excel的名称

		filename = MyUtils.encodeFilename(filename, request);// 处理中文文件名
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition", "attachment;filename="
				+ filename);
		OutputStream ouputStream = response.getOutputStream();
		workbook.write(ouputStream);
		ouputStream.flush();
		ouputStream.close();
	}
}
